﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LotTemplateBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILotTemplateDA lotTemplateDA = DataAccess.CreateLotTemplateDA();
        private PubParasInfo pubParasInfo;

        public LotTemplateBLL()
        {
            this.pubParasInfo = null;
        }

        public LotTemplateBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LotTemplateBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> LotTemplateID)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<LotTemplateInfo> lotTemplateInfos = new List<LotTemplateInfo>();
                LotTemplateInfo lotTemplateInfo = null;
                foreach (string id in LotTemplateID)
                {
                    lotTemplateInfo = lotTemplateDA.GetLotTemplateByID(dataBase, tran, id, this.dataBaseID[0]);
                    lotTemplateInfos.Add(lotTemplateInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (lotTemplateDA.DeleteLotTemplate(dataBase, tran, LotTemplateID, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < LotTemplateID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<LotTemplateInfo>(dataBase, tran, lotTemplateInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LOT_TEMPLATE", this.dataBaseID);
                        temp = temp + LotTemplateID[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return lotTemplateDA.GetAllLotTemplate(this.dataBaseID[0]);
        }

        public object GetInfoByID(string LotTemplateID)
        {
            return lotTemplateDA.GetLotTemplateByID(LotTemplateID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            LotTemplateQueryEntity lotTemplateQueryEntityQuery = obj as LotTemplateQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(lotTemplateQueryEntityQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + lotTemplateQueryEntityQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(lotTemplateQueryEntityQuery.LotTemplateID))
            {
                sqlWhere.Add(" LOT_TEMPLATE_ID " + lotTemplateQueryEntityQuery.LotTemplateID);
            }
            if (!string.IsNullOrEmpty(lotTemplateQueryEntityQuery.Description))
            {
                sqlWhere.Add(" DESCRIPTION " + lotTemplateQueryEntityQuery.Description);
            }
            return lotTemplateDA.GetLotTemplateByQueryList(sqlWhere, lotTemplateQueryEntityQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string LotTemplateID = string.Empty;
            try
            {
                LotTemplateInfo lotTemplate = obj as LotTemplateInfo;
                if (lotTemplateDA.CheckLotTemplateIDUnique(lotTemplate.LotTemplateID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2230_001", this.pubParasInfo.UserLanguage, new List<string> { lotTemplate.LotTemplateID });
                }
                lotTemplate.CreatedDate = DateTime.Now;
                lotTemplate.UpdatedDate = DateTime.Now;
                lotTemplate.UpdatedBy = lotTemplate.CreatedBy;
                if (lotTemplateDA.InsertLotTemplate(lotTemplate, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LotTemplateInfo>(null, lotTemplate, AuditAction.Insert, this.pubParasInfo.UserID, "LOT_TEMPLATE", this.dataBaseID[0]);
                    LotTemplateID = lotTemplate.LotTemplateID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LotTemplateID;
        }

        public string Update(object obj)
        {
            string LotTemplateID = string.Empty;
            try
            {
                LotTemplateInfo lotTemplate = obj as LotTemplateInfo;
                lotTemplate.UpdatedDate = DateTime.Now;
                LotTemplateInfo oldLotTemplateInfo = lotTemplateDA.GetLotTemplateByID(lotTemplate.LotTemplateID, this.dataBaseID[0]);
                if (lotTemplateDA.UpdateLotTemplate(lotTemplate, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LotTemplateInfo>(oldLotTemplateInfo, lotTemplate, AuditAction.Update, this.pubParasInfo.UserID, "LOT_TEMPLATE", this.dataBaseID[0]);
                    LotTemplateID = lotTemplate.LotTemplateID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LotTemplateID;
        }
    }
}

